package com.lc.railway.ui.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.lc.base.BaseDialog;
import com.lc.railway.R;
import com.lc.railway.http.response.NavListBean;
import com.lc.railway.other.IntentKey;
import com.lc.railway.ui.activity.DutyIndexActivity;
import com.lc.railway.ui.activity.ElegantActivity;
import com.lc.railway.ui.activity.HonorListActivity;
import com.lc.railway.ui.activity.IssueActivity;
import com.lc.railway.ui.activity.LinksActivity;
import com.lc.railway.ui.activity.MailboxActivity;
import com.lc.railway.ui.activity.NewsDeatailsActivity;
import com.lc.railway.ui.activity.NewsSecondaryActivity;
import com.lc.railway.ui.activity.VideoActivity;
import com.lc.railway.ui.activity.WebViewActivity;
import com.lc.railway.util.SPUtil;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.ButterKnife;

public class FieldsDialog extends BaseDialog {
    private Context context;
    private String title;
    private Activity activity;
    private RecyclerView recyclerView;

    //    private BaseQuickAdapter<TimeBean, BaseViewHolder> adapter;
    public FieldsDialog(Context context, String title) {
        super(context, R.style.RightAnimStyle);
        this.context = context;
        this.title=title;


    }
    public FieldsDialog(Context context,Activity activity, String title) {
        super(context, R.style.RightAnimStyle);
        this.context = context;
        this.activity = activity;
        this.title=title;


    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);

        Window window = getWindow();
        window.setGravity(Gravity.CENTER); // 此处可以设置dialog显示的位置为居中
//        window.setWindowAnimations(R.style.bottom_menu_animation); // 添加动画效果
        setContentView(R.layout.dialog_more);

        WindowManager windowManager = ((Activity) context).getWindowManager();
//        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(lp);
        setCanceledOnTouchOutside(false);// 点击Dialog外部消失



        initView();


    }

    private void initView() {
        TitleBar titleBar=findViewById(R.id.title_bar_top);
        titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {

            }

            @Override
            public void onTitleClick(View v) {

            }

            @Override
            public void onRightClick(View v) {
               dismiss();
            }
        });
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(context,3));
       List<NavListBean.DataBean> list= JSON.parseArray(SPUtil.get(context, IntentKey.TEXT,""),NavListBean.DataBean.class);

        for (int i = 0; i <list.size() ; i++) {
            if (title.equals(list.get(i).getClassname())){
                  list.get(i).setGrop(true);
            }
        }
        BaseQuickAdapter<NavListBean.DataBean, BaseViewHolder> navadapter=new BaseQuickAdapter<NavListBean.DataBean, BaseViewHolder>(R.layout.item_lanmu,list) {
            @Override
            protected void convert(@NotNull BaseViewHolder holder, NavListBean.DataBean dataBean) {
                  holder.setText(R.id.tv_title,dataBean.getClassname());
                  if (dataBean.isGrop()){
                   holder.setBackgroundColor(R.id.tv_title,getResources().getColor(R.color.main_color));
                      holder.setTextColor(R.id.tv_title,getResources().getColor(R.color.white));
                  }else {
                      holder.setBackgroundColor(R.id.tv_title,getResources().getColor(R.color.colorf6f6f6));
                      holder.setTextColor(R.id.tv_title,getResources().getColor(R.color.color454545));
                  }
            }
        };
        recyclerView.setAdapter(navadapter);
        navadapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                if (navadapter.getData().get(position).getClassname().equals("警营风采")){
                    startActivity( ElegantActivity.class);
                }else if(navadapter.getData().get(position).getClassname().equals("荣誉榜")){

                    startActivity(HonorListActivity.class);
                }else if(navadapter.getData().get(position).getClassname().equals("局长邮箱")){
                    startActivity(new Intent(getContext(), MailboxActivity.class).putExtra("type","局长"));
                }else if(navadapter.getData().get(position).getClassname().equals("政委邮箱")){
                    startActivity(new Intent(getContext(),MailboxActivity.class).putExtra("type","政委"));
                }else if(navadapter.getData().get(position).getClassname().equals("发布榜单")){
                    startActivity(new Intent(getContext(), IssueActivity.class));
                } else if(navadapter.getData().get(position).getClassname().equals("值班列表")){
                    startActivity(DutyIndexActivity.class);
                }else if(navadapter.getData().get(position).getClassname().equals("视频")){
                    startActivity(VideoActivity.class);
                }else if(navadapter.getData().get(position).getClassname().equals("友情链接")){
                    startActivity(LinksActivity.class);
                }
                else if(navadapter.getData().get(position).getClassname().equals("通讯录")){

                    startActivity(new Intent(getContext(), NewsDeatailsActivity.class).putExtra("id", navadapter.getData().get(position).getId() + "").putExtra("title", navadapter.getData().get(position).getClassname()).putExtra("type","3"));
                }else if (navadapter.getData().get(position).getClassname().equals("电子邮箱")){
                    startActivity(WebViewActivity.class);
                }
                else {
                    startActivity(new Intent(getContext(), NewsSecondaryActivity.class).putExtra("id", navadapter.getData().get(position).getId()+"").putExtra("title",navadapter.getData().get(position).getClassname()));
                }
                dismiss();
                if (activity!=null){
                    activity.finish();
                }

            }
        });
    }

}
